<?php

//以UTF-8编码显示
function utf8() {
    header("Content-type:text/html;charset=utf-8");
}

//获取仓库的信息
function getCommit($url = 'https://gitee.com/api/v5/repos/https:/w169cn/123/commits?page=1&per_page=200') {
    // 初始化curl工具
    $curl = curl_init($url);

    // 进行必要的配置
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 取消curl显示返回内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    // 跳过证书检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    //从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    // 执行curl
    $data = curl_exec($curl);
    // 对data进行解码，将其从json格式转化为php数组
    $data = json_decode($data, true);
    curl_close($curl);
    return $data;
}

//获得提交时间信息
function getCommitByWeek(array $data) {
    // 提取每一次作业的提交时间
    $commitDate = array();
    foreach ($data as $key => $value) {
        $commitDate[$key] = strtotime($data[$key]['commit']['commiter']['date']);
    }
    return $commitDate;
}

//周次节点信息
function weekly($start = '2018-03-05') {
    for ($i = 0; $i < 19; $i++) {
        $data[$i] = strtotime("+" . $i . " week", strtotime($start));
    }
    return $data;
}

//获取每周作业推送次数
function getCountByWeek(array $commitDate = array()) {
    // 先得到学期时间节点
    $week = weekly();
    // 初始化数组
    $array = array();
    // 逐一比较
    for ($i = 0; $i < 19; $i++) {
        $array[$i] = 0;
        foreach ($commitDate as $key => $value) {
            if ($value >= $week[$i] && $value <= $week[$i + 1]) {
                $array[$i]++;
            }
        }
    }
    return $array;
}

//生成作业报告
function report(array $data, array $countByWeek) {
    $chengji = 0;
    echo '我的作业地址是：' . '<br>';
    echo '提交次数：' . count($data) . '次。<br>';
    foreach ($countByWeek as $key => $value) {
        echo '第' . ($key + 1) . '周提交作业' . $value . '次。<br>';
        //判断是否提交和总成绩是否超过100
        if($value > 0 && $chengji < 100){
            $chengji = $chengji + 10;
        }
    }
    echo '我的总成绩为：' . $chengji . '<br>';
}

//运行部分
utf8();
$data = getCommit();
$commitDate  = getCommitByWeek($data);
$countByWeek = getCountByWeek($commitDate);
report($data, $countByWeek);
?>
